package com.lefevre.cms.web.action.user;

import com.lefevre.cms.bean.RequestResult;
import com.lefevre.cms.bean.ResultCode;
import com.lefevre.cms.bean.user.PointLog;
import com.lefevre.cms.bean.user.User;
import com.lefevre.cms.service.user.UserService;
import com.lefevre.cms.utils.JsonUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * 积分日志管理
 */
@Controller
@RequestMapping("/control/pointLog/manage")
public class PointLogManageAction {
    @Resource
    PointManage pointManage;
    @Resource
    UserService userService;

    /**
     * 积分日志管理 详细显示
     *
     * @param model
     * @param pointLogId
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(params = "method=show", method = RequestMethod.GET)
    public String addUI(ModelMap model, String pointLogId, String userName,
                        HttpServletRequest request, HttpServletResponse response) throws Exception {

        Map<String, String> error = new HashMap<>();
        Map<String, Object> returnValue = new HashMap<>();
        if (pointLogId != null && !"".equals(pointLogId.trim())) {
            if (pointLogId.trim().length() == 36 && pointManage.verificationPointLogId(pointLogId)) {
                PointLog pointLog = userService.findPointLogById(pointLogId);
                User _user = userService.findUserByUserName(userName);
                if (_user != null) {
                    returnValue.put("currentUser", _user);
                }

                returnValue.put("pointLog", pointLog);
            }
        } else {
            error.put("pointLogId", "积分Id不能为空");
        }
        if (error.size() > 0) {
            return JsonUtils.toJSONString(new RequestResult(ResultCode.FAILURE, error));
        } else {
            return JsonUtils.toJSONString(new RequestResult(ResultCode.SUCCESS, returnValue));
        }
    }

}

